package com.liucc.gulimall.product.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liucc.common.utils.PageUtils;
import com.liucc.common.utils.Query;

import com.liucc.gulimall.product.dao.CategoryDao;
import com.liucc.gulimall.product.entity.CategoryEntity;
import com.liucc.gulimall.product.service.CategoryService;


@Service("categoryService")
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntities = categoryDao.selectList(null);

        // 封装为树形结构
        List<CategoryEntity> tree = categoryEntities.stream().filter(item -> item.getParentCid().equals(0L)).map(menu -> {
            // 递归找到分类集合中当前分类所有的子分类
            menu.setChildren(getChildren(menu, categoryEntities));
            return menu;
        }).sorted((menu1, menu2) -> {
            //2、菜单的排序
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return tree;
    }

    // 【2 25 225】
    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> longs = recursionFindPath(catelogId, new ArrayList());
        Collections.reverse(longs);
        Long[] paths = longs.toArray(new Long[]{});
        log.info("当前分类id的全路径是：{}", Arrays.asList(paths));
        return paths;
    }

    /**
     * 【2 25 225】
     * @param catelogId 当前分类id
     * @param paths 收集当前分类id的父路径
     * @return
     */
    private List<Long> recursionFindPath(Long catelogId, List<Long> paths){
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        paths.add(catelogId);
        if (categoryEntity.getParentCid() != 0) {
            // 递归查找
            recursionFindPath(categoryEntity.getParentCid(), paths);
        }
        return paths;
    }

    /**
     * 递归找到分类集合中当前分类所有的子分类
     * @param menu
     * @param all
     * @return
     */
    private List<CategoryEntity> getChildren(CategoryEntity menu, List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
//            return categoryEntity.getParentCid() == menu.getCatId();
            return categoryEntity.getParentCid().equals(menu.getCatId());
        }).map(categoryEntity -> {
            //1、找到子菜单
            categoryEntity.setChildren(getChildren(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            //2、菜单的排序
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}